package org.jsbd.boss.util;

import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

public class PhoneUtil {
	private static Random r = new Random();
	private static final String[] RELEASE = { "2.3.6", "4.0.4", "4.4.2", "4.2.1", "4.3", "4.4.4", "4.1.2", "4.1.1" };
	private static final String[] HSTYPE = { "vivo Y27", "SM-G5108", "HTC D816w", "MIU-H6", "gucci", "R823T",
			"R7Plusm", "HM NOTE 1LTETD", "SM-G3508I", "SM-G3508J", "R831T", "Lenovo A656", "SM-G9008V",
			"HUAWEI Y511-T00", "H30-U10", "Coolpad 5200S", "X905", "S39h", "Coolpad 5200", "ALE-TL00", "SHV-E210L",
			"CRAVE S1", "Lenovo A398t+", "S2", "GT-I8558", "HM 1S" };

    /**
     * 根据imsi 获取手机号段
     * 
     * @param imsi
     * @return 7位手机号段 如果计算不出 返回空
     */
    public static String getMobileByIMSI(String imsi) {

        try {
            Double.parseDouble(imsi);
        } catch (Exception e) {
            return "";
        }

        String moblie = "";

        int index = 0;

        if (imsi != null && !"".equals(imsi) && imsi.length() > 10) {
            String mnc = imsi.substring(0, 5);
            String msin = imsi.substring(5, imsi.length());
            int nMNC = Integer.parseInt(mnc);
            switch (nMNC) {
            // 移动
            case 46000:
                index = Integer.parseInt(msin.substring(3, 4)); // 13S号码段
                if (index >= 5 && index <= 9) {
                    moblie = "13";
                    moblie += index;
                    moblie += "0";
                    moblie += msin.substring(0, 3);
                } else {
                    moblie = "13";
                    moblie += index + 5;
                    moblie += msin.substring(4, 5);
                    moblie += msin.substring(0, 3);
                }
                break;
            case 46002:
                index = Integer.parseInt(msin.substring(0, 1));
                switch (index) {
                case 0:
                    if (Integer.parseInt(msin.substring(1, 2)) <= 8) {
                        moblie = "134" + msin.substring(1, 5);
                    }
                    break;
                case 1:
                case 2:
                case 8:
                case 9:
                    moblie = "15" + index + msin.substring(1, 5);
                    break;
                case 3:
                    moblie = "150" + msin.substring(1, 5);
                    break;
                case 5:
                    moblie = "183" + msin.substring(1, 5);
                    break;
                case 6:
                    moblie = "182" + msin.substring(1, 5);
                    break;
                case 7:
                    moblie = "187" + msin.substring(1, 5);
                    break;
                }
                break;
            case 46007:
                index = Integer.parseInt(msin.substring(0, 1));
                switch (index) {
                case 7:
                    moblie = "157" + msin.substring(1, 5);
                    break;
                case 8:
                    moblie = "188" + msin.substring(1, 5);
                    break;
                case 9:
                    moblie = "147" + msin.substring(1, 5);
                    break;
                }
                break;
            // 联通
            case 46001:
                index = Integer.parseInt(msin.substring(4, 5));
                switch (index) {
                // 13段
                case 0:
                case 1:
                    moblie += "130";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                case 9:
                    moblie += "131";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                case 2:
                    moblie += "132";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                // 15段
                case 3:
                    moblie += "156";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                case 4:
                    moblie += "155";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                // 18段
                case 6:
                    moblie += "186";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;
                case 5:
                    moblie += "185";
                    moblie += msin.substring(3, 4);
                    moblie += msin.substring(0, 3);
                    break;

                }
                break;
            // 电信
            case 46003:
                if (msin.substring(0, 1).equals("0")) {
                    index = Integer.parseInt(msin.substring(1, 2));
                    switch (index) {
                    case 9:
                        if ("00".equals(msin.substring(2, 4))) {
                            moblie += "13301";
                            moblie += msin.substring(4, 6);
                        } else if ("53".equals(msin.substring(2, 4)) || "54".equals(msin.substring(2, 4))) {
                            moblie += "133";
                            moblie += Integer.parseInt(msin.substring(2, 6)) + 4500;
                        } else {
                            moblie += "133";
                            moblie += msin.substring(2, 6);
                        }
                        break;
                    case 3:
                        moblie += "133";
                        moblie += Integer.parseInt(msin.substring(2, 6)) + 5000;
                        break;
                    }
                } else {
                    moblie += "153";
                    moblie += msin.substring(1, 3);
                    moblie += msin.substring(4, 6);
                }
                break;

            default:
                break;
            }
        }
        return moblie;
    }

    public static String getIMEI() {// calculator IMEI
        int r1 = 1000000 + new java.util.Random().nextInt(9000000);
        int r2 = 1000000 + new java.util.Random().nextInt(9000000);
        String input = r1 + "" + r2;
        char[] ch = input.toCharArray();
        int a = 0, b = 0;
        for (int i = 0; i < ch.length; i++) {
            int tt = Integer.parseInt(ch[i] + "");
            if (i % 2 == 0) {
                a = a + tt;
            } else {
                int temp = tt * 2;
                b = b + temp / 10 + temp % 10;
            }
        }
        int last = (a + b) % 10;
        if (last == 0) {
            last = 0;
        } else {
            last = 10 - last;
        }
        return input + last;
    }
    
    
    /**
     * 根据移动手机号段获取imsi
     * 额外说明：因无法完全模拟器imsi,该规则不一定准确,只是比随机好点
     * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
     * @param imsi
     * @return 
     */
    public static String getImsiByYdMobile(String phoneNum) {
    	String imsi=changeYdPhoneToImsi(phoneNum);
    	if(StringUtils.isNotBlank(imsi)){
    		if(imsi.length()==15){
    			return imsi;
    		}
    	}
        return null;
    }
    
    public static String changeYdPhoneToImsi(String phoneNum){
    	
    	if(!NumberUtils.isNumber(phoneNum)){
    		return null;
    	}
    	
        StringBuffer imsi = new StringBuffer();
        
        if(phoneNum.startsWith("147")){
        	imsi.append("460079").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        
        if(phoneNum.startsWith("188")){
        	imsi.append("460078").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        
        if(phoneNum.startsWith("157")){
        	imsi.append("460077").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        
        if(phoneNum.startsWith("150")){
        	imsi.append("460023").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        
        if(phoneNum.startsWith("183")){
        	imsi.append("460025").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        if(phoneNum.startsWith("182")){
        	imsi.append("460026").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	return imsi.toString();
        }
        if(phoneNum.startsWith("187")){
        	imsi.append("460027").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
            return imsi.toString();
        }
        
        //这个要放在150、157后面
        if(phoneNum.startsWith("15")){
        	imsi.append("46002").append(phoneNum.substring(2,7)).append(MathUtils.getRandomString(5));
        	 return imsi.toString();
        }
        
        if(phoneNum.startsWith("134")){
        	imsi.append("460020").append(phoneNum.substring(3,7)).append(MathUtils.getRandomString(5));
        	 return imsi.toString();
        }
        
        //这个要放在134后面
        if(phoneNum.startsWith("13")){
        	
        	int num=Integer.valueOf(phoneNum.substring(2,3));
        	if(num>=5){
        		num=num-5;
        	}
        	imsi.append("46000").append(phoneNum.substring(4,7)).append(num).append(phoneNum.substring(3,4)).append(MathUtils.getRandomString(5));
            return imsi.toString();
        }
        return null;
    }
    
    
    public static String getRelease() {
		return RELEASE[r.nextInt(RELEASE.length)];
	}

	public static String getHstype() {
		return HSTYPE[r.nextInt(HSTYPE.length)];
	}

	public static String getBuild() {
		return UUID.randomUUID().toString().substring(0, 6).toUpperCase();
	}
    
}
